<?php
// Class that implements the DB abstraction for mysql

class db_mysql implements db {
 private $link;
 private $result;

 public function __construct($server, $username, $password)
 {
  $this->link = mysql_connect($server, $username, $password);
  if (!$this->link)
  {
  	die('Could not connect: ' . mysql_error());
  }
 }
 
 public function whichDB()
 {
 	return "mysql";
 }

 public function fetchResult()
 {
 	return mysql_fetch_array($this->result, MYSQL_ASSOC);
 }
 
 public function selectDatabase($dbName)
 {
 	$this->result = mysql_select_db($dbName, $this->link);
 	if (!$this->result)
 	{
   		die ('Can\'t use the table : ' . mysql_error());
  	}
 }

 public function insertValues($tableName, $record)
 {
 	$query = sprintf("INSERT INTO %s VALUES (%s)", 
    			$tableName,
    			$record);

  	if (!($this->result = mysql_query($query, $this->link)))
  	{
    	die('Error: ' . mysql_error());
  	}
  }

 public function sendSelectQuery($tableName, $fields, $whereClause)
 {
	$query = sprintf("SELECT %s FROM %s WHERE %s",
	$fields,
	$tableName,
	$whereClause
	);
	
	if (!($this->result = mysql_query($query, $this->link)))
	{
		die('Error: ' . mysql_error());
	}
 }

 public function countRows()
 {
   return mysql_num_rows($this->result);
 }

}

